package com.lanhe.common.utils.qingchuang;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.qingchuang.dto.QingChuangApplyDTO;
import com.lanhe.common.utils.qingchuang.dto.QingChuangOrderDTO;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 轻创优选
 *
 * @author zhanggl
 * @date 2023年12月11日23:19:26
 */
@Component
@Slf4j
public class QingChuangUtil {

	/**
	 * -- GETTER --
	 * 获取配置文件
	 *
	 * @return java.lang.String
	 */
	@Getter
	@Value("${lanhe.qingchuang.appSecret}")
	private String appSecret;

	@Value("${lanhe.qingchuang.listURL}")
	private String listURL;

	@Value("${lanhe.qingchuang.infoURL}")
	private String infoURL;

	@Value("${lanhe.qingchuang.applyURL}")
	private String applyURL;

	@Value("${lanhe.qingchuang.applyListURL}")
	private String applyListURL;

	@Value("${lanhe.qingchuang.suborderURL}")
	private String suborderURL;

	/**
	 * 广告列表
	 *
	 * @param keyword 关键词
	 * @param type    1提交任务,2结算任务
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/4
	 */
	public String getQingChuangProductList(String keyword, Integer type) throws Exception {

		long startTime = System.currentTimeMillis();

		Map<String, Object> params = new HashMap<>();
		if (ObjectUtil.isNotNull(type)){
			params.put("type", type);
		}
		if (StringUtils.isNotBlank(keyword)){
			params.put("keyword", keyword);
		}

		HttpRequest request = HttpRequest.post(listURL);
		request.header("User-Agent", "Apifox/1.0.0 (https://apifox.com)").header("apikey", appSecret);
		request.form(params);
		String result = request.execute().body();
		log.info("[{}] [getQingChuangProductList] ======>请求 {}", this.getClass().getSimpleName(),
				listURL);
		log.info("[{}] [getQingChuangProductList] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getQingChuangProductList] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 广告列表-详情
	 *
	 * @param number 任务编号
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023年12月12日00:47:44
	 */
	public String getQingChuangProductInfo(String number) throws Exception {

		long startTime = System.currentTimeMillis();

		Map<String, Object> params = new HashMap<>();
		params.put("number", number);
		HttpRequest request = HttpRequest.post(infoURL);
		request.header("User-Agent", "Apifox/1.0.0 (https://apifox.com)").header("apikey", appSecret);
		request.form(params);
		String result = request.execute().body();
		log.info("[{}] [getQingChuangProductInfo] ======>请求 {}", this.getClass().getSimpleName(),
				listURL);
		log.info("[{}] [getQingChuangProductInfo] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [getQingChuangProductInfo] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}


	/**
	 * 报备信息
	 *
	 * @param applyDto 入参
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/15
	 */
	public String apply(QingChuangApplyDTO applyDto) {

		long startTime = System.currentTimeMillis();

		Map<String, Object> params = new HashMap<>();
		params.put("number", applyDto.getNumber());
		params.put("uid", applyDto.getUid());
		params.put("mobile", applyDto.getMobile());

		Map<String,Object> textMap = new HashMap<>();
		for (QingChuangApplyDTO.TextBean textBean : applyDto.getText()) {
			textMap.put(textBean.getName(),textBean.getValue());
		}
		params.put("text", new JSONObject(textMap));
		HttpRequest request = HttpRequest.post(applyURL);
		request.header("User-Agent", "Apifox/1.0.0 (https://apifox.com)").header("apikey", appSecret);
		request.body(JSON.toJSONString(params));
		String result = request.execute().body();
		log.info("[{}] [apply] ======>请求 {}", this.getClass().getSimpleName(),
				params);
		log.info("[{}] [apply] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [apply] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}

	/**
	 * 报备数据列表
	 *
	 * @param userKey  用户KEY
	 * @param pid      产品ID
	 * @param status   状态
	 * @param pageNum  页面码
	 * @param pageSize 条数
	 * @return 结果
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	public String applyList(String userKey, Long pid, Long status, Integer pageNum, Integer pageSize) {

		long startTime = System.currentTimeMillis();

		Map<String, Object> params = new HashMap<>();
		params.put("uid", userKey);
		params.put("pid", pid);
		params.put("status", status);
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		HttpRequest request = HttpRequest.post(applyListURL);
		request.header("User-Agent", "Apifox/1.0.0 (https://apifox.com)").header("apikey", appSecret);
		request.body(JSON.toJSONString(params));
		String result = request.execute().body();
		log.info("[{}] [applyList] ======>请求 {}", this.getClass().getSimpleName(),
				params);
		log.info("[{}] [applyList] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [applyList] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}


	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}


	/**
	 * 提交信息信息
	 *
	 * @param orderDTO 入参
	 * @return java.lang.String
	 * @author zhanggl
	 * @date 2023/12/15
	 */
	public String suborder(QingChuangOrderDTO orderDTO) {

		long startTime = System.currentTimeMillis();

		Map<String, Object> params = new HashMap<>();
		params.put("number", orderDTO.getNumber());
		params.put("uid", orderDTO.getUid());
		params.put("order_no", orderDTO.getOrderNo());
		params.put("applyid", orderDTO.getApplyId());
		Map<String,Object> formMap = new HashMap<>();
		for (QingChuangOrderDTO.FormBean formBean : orderDTO.getForm()) {
			formMap.put(formBean.getName(),formBean.getValue());
		}
		params.put("form", new JSONObject(formMap));

		HttpRequest request = HttpRequest.post(suborderURL);
		request.header("User-Agent", "Apifox/1.0.0 (https://apifox.com)").header("apikey", appSecret);
		request.body(JSON.toJSONString(params));
		String result = request.execute().body();
		log.info("[{}] [suborder] ======>请求 {}", this.getClass().getSimpleName(),
				params);
		log.info("[{}] [suborder] ======>返回 {}", this.getClass().getSimpleName(),
				result);
		log.info("[{}] [suborder] ======>耗时 {}", this.getClass().getSimpleName(),
				(System.currentTimeMillis() - startTime));
		return result;
	}
}
